View Javadoc
1   package org.apache.maven.surefire.junitcore.pc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.report.ConsoleLogger;
23  
24  import java.util.concurrent.ExecutorService;
25  import java.util.concurrent.Executors;
26  
27  /**
28   * The factory of {@link SchedulingStrategy}.
29   *
30   * @author Tibor Digana (tibor17)
31   * @since 2.16
32   */
33  public class SchedulingStrategies
34  {
35  
36      /**
37       * @param logger current error logger
38       * @return sequentially executing strategy
39       */
40      public static SchedulingStrategy createInvokerStrategy( ConsoleLogger logger )
41      {
42          return new InvokerStrategy( logger );
43      }
44  
45      /**
46       * @param logger current error logger
47       * @param nThreads fixed pool capacity
48       * @return parallel scheduling strategy
49       */
50      public static SchedulingStrategy createParallelStrategy( ConsoleLogger logger, int nThreads )
51      {
52          return new NonSharedThreadPoolStrategy( logger, Executors.newFixedThreadPool( nThreads ) );
53      }
54  
55      /**
56       * @param logger current error logger
57       * @return parallel scheduling strategy with unbounded capacity
58       */
59      public static SchedulingStrategy createParallelStrategyUnbounded( ConsoleLogger logger )
60      {
61          return new NonSharedThreadPoolStrategy( logger, Executors.newCachedThreadPool() );
62      }
63  
64      /**
65       * The <tt>threadPool</tt> passed to this strategy can be shared in other strategies.
66       * <p/>
67       * The call {@link SchedulingStrategy#finished()} is waiting until own tasks have finished.
68       * New tasks will not be scheduled by this call in this strategy. This strategy is not
69       * waiting for other strategies to finish. The {@link org.junit.runners.model.RunnerScheduler#finished()} may
70       * freely use {@link SchedulingStrategy#finished()}.
71       *
72       * @param logger current error logger
73       * @param threadPool thread pool possibly shared with other strategies
74       * @return parallel strategy with shared thread pool
75       * @throws NullPointerException if <tt>threadPool</tt> is null
76       */
77      public static SchedulingStrategy createParallelSharedStrategy( ConsoleLogger logger, ExecutorService threadPool )
78      {
79          if ( threadPool == null )
80          {
81              throw new NullPointerException( "null threadPool in #createParallelSharedStrategy" );
82          }
83          return new SharedThreadPoolStrategy( logger, threadPool );
84      }
85  }